import {Material,Cartesian4, Color} from '@cesiumjs';

var MaterialType = (0, function() {
  'use strict'; //import Cesium from 'Cesium';
  const source = `\nuniform sampler2D specularMap;\n
      uniform sampler2D normalMap;\n
      uniform vec4 baseWaterColor;\n
      uniform vec4 blendColor;\n
      uniform float frequency;\n
      uniform float animationSpeed;\n
      uniform float amplitude;\n
      uniform float specularIntensity;\n
      uniform float fadeFactor;\n
      uniform vec4 sizeAndVelocity;\n\n
      czm_material czm_getMaterial(czm_materialInput materialInput){\n
          float width = sizeAndVelocity.x;\n
          float height = sizeAndVelocity.y;\n
          float vx = sizeAndVelocity.z;\n
          float vy = sizeAndVelocity.w;\n
          czm_material material = czm_getDefaultMaterial(materialInput);\n\n
          float time = czm_frameNumber * animationSpeed;\n\n
          // fade is a function of the distance from the fragment and the frequency of the waves\n
          float fade = max(1.0, (length(materialInput.positionToEyeEC) / 10000000000.0) * frequency * fadeFactor);\n\n
          // note: not using directional motion at this time, just set the angle to 0.0;\n
          vec2 st = materialInput.st * vec2(width, height) / 100.0 * frequency;\n
          st -= vec2(vx*time, vy*time);\n
          vec4 noise = czm_getWaterNoise(normalMap, st, time, 0.0);\n
          vec3 normalTangentSpace = noise.xyz * vec3(1.0, 1.0, (1.0 / amplitude));\n\n
          // fade out the normal perturbation as we move further from the water surface\n
          normalTangentSpace.xy /= fade;\n
          normalTangentSpace = normalize(normalTangentSpace);\n\n
          // get ratios for alignment of the new normal vector with a vector perpendicular to the tangent plane\n
          float tsPerturbationRatio = clamp(dot(normalTangentSpace, vec3(0.0, 0.0, 1.0)), 0.0, 1.0);\n\n
          // base color is a blend of the water and non-water color based on the value from the specular map\n
          // may need a uniform blend factor to better control this\n
          vec2 v = gl_FragCoord.xy / czm_viewport.zw;\n
          v.y = 1.0 - v.y;\n
          material.diffuse = texture2D(specularMap, v + noise.xy*0.03).rgb;\n\n
          // diffuse highlights are based on how perturbed the normal is\n
          material.diffuse += (0.1 * tsPerturbationRatio);\n\n
          material.diffuse = mix(baseWaterColor.rgb, material.diffuse, blendColor.rgb);\n
          material.normal = normalize(materialInput.tangentToEyeMatrix * normalTangentSpace);\n\n
          material.specular = specularIntensity;\n
          material.shininess = 10.0;\n
          material.alpha = baseWaterColor.a * blendColor.a;\n\n
          return material;\n
      }\n`;

  const type = 'G3xWater';
  const $Material = Material;
  return {
    type,
    init() {
      if (!$Material.G3xWaterType) {
        $Material.G3xWaterType = type;
        $Material._materialCache.addMaterial(type, {
          fabric: {
            type,
            source,
            'uniforms': {
              'baseWaterColor': new Color(0.2, 0.3, 0.6, 0x1),
              'blendColor': new Color(0.5, 0.5, 0.5, 0.7),
              'specularMap': $Material.DefaultImageId,
              'normalMap': $Material.DefaultImageId,
              'frequency': 0x1388,
              'animationSpeed': 0.05,
              'amplitude': 0x2,
              'specularIntensity': 0.5,
              'fadeFactor': 0x1,
              'sizeAndVelocity': new Cartesian4(0x64, 0x64, 0xa, 0x0)
            }
          },
          'translucent': function(src) {
            var uniforms = src.uniforms;
            return uniforms.baseWaterColor.alpha < 1 || uniforms.blendColor.alpha < 1;
          }
        });
      }
      return true;
    }
  }
})()

/**
 * import { WaterType } from "@g3v/Materials";
 * WaterType.init();
 */
export default MaterialType;


//Cesium 预设：webpack:///./node_modules/_cesium@1.83.0@cesium/Source/Scene/Material.js?c805
/**
 * Gets the name of the water material.
 * @type {String}
 * @readonly
Material.WaterType = "Water";
Material._materialCache.addMaterial(Material.WaterType, {
  fabric: {
    type: Material.WaterType,
    uniforms: {
      baseWaterColor: new Color(0.2, 0.3, 0.6, 1.0),
      blendColor: new Color(0.0, 1.0, 0.699, 1.0),
      specularMap: Material.DefaultImageId,
      normalMap: Material.DefaultImageId,
      frequency: 10.0,
      animationSpeed: 0.01,
      amplitude: 1.0,
      specularIntensity: 0.5,
      fadeFactor: 1.0,
    },
    source: WaterMaterial,
  },
  translucent: function (material) {
    var uniforms = material.uniforms;
    return (
      uniforms.baseWaterColor.alpha < 1.0 || uniforms.blendColor.alpha < 1.0
    );
  },
});
 */
